package com.example.fbi_text.views;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.example.fbi_text.Acitivity.LoginActivity;
import com.example.fbi_text.Acitivity.SettingsActivity;
import com.example.fbi_text.R;
import com.example.fbi_text.utils.SpUtils;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private Mainfragment mianfragment;
    private Myselffragment myselffragment;
    private ProductListFragment productListFragment;
    private Fragment mContentFragment;
    private FragmentManager mFriendsFragment;
    private ImageView buttommain;
    private ImageView buttomshop;
    private ImageView buttommyself;
    private SpUtils spUtils;
    private TextView textHome, textShop, textMyself;

    @SuppressLint({"WrongViewCast", "MissingInflatedId"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        buttommain = findViewById(R.id.buttom_shouye);
        buttomshop = findViewById(R.id.buttom_shop);
        buttommyself = findViewById(R.id.buttom_myself);

        // 初始化文字
        textHome = findViewById(R.id.text_home);
        textShop = findViewById(R.id.text_shop);
        textMyself = findViewById(R.id.text_myself);

        buttommain.setOnClickListener(this);
        buttomshop.setOnClickListener(this);
        buttommyself.setOnClickListener(this);
        initFragment();
        changeFragment(mianfragment);
        mFriendsFragment.beginTransaction()
                .add(R.id.fragment_content, mianfragment).commitAllowingStateLoss();
        mContentFragment = mianfragment;
    }
//      点击事件，通过id寻找哪个网页
    @Override
    public void onClick(View v) {
        resetAllTabs(); // 重置所有图标和文字
        if (v.getId() == R.id.buttom_shouye) {
            setSelectedTab(buttommain, textHome, R.mipmap.home);
            changeFragment(mianfragment);
        } else if (v.getId() == R.id.buttom_shop) {
            setSelectedTab(buttomshop, textShop, R.mipmap.shop_in);
            changeFragment(productListFragment);
        } else if (v.getId() == R.id.buttom_myself) {
            setSelectedTab(buttommyself, textMyself, R.mipmap.login_in);
            changeFragment(myselffragment);
        }
    }
    private void resetAllTabs() {
        // 重置图标
        buttommain.setImageResource(R.mipmap.home_notin);
        buttomshop.setImageResource(R.mipmap.shop_icon);
        buttommyself.setImageResource(R.mipmap.login_notin);

        // 重置文字颜色
        textHome.setTextColor(getResources().getColor(R.color.white));
        textShop.setTextColor(getResources().getColor(R.color.white));
        textMyself.setTextColor(getResources().getColor(R.color.white));
    }

    private void setSelectedTab(ImageView icon, TextView text, int iconRes) {
        icon.setImageResource(iconRes);
        text.setTextColor(getResources().getColor(R.color.black));
    }

    private void resetTextColors() {
        textHome.setTextColor(getResources().getColor(R.color.white));
        textShop.setTextColor(getResources().getColor(R.color.white));
        textMyself.setTextColor(getResources().getColor(R.color.white));
    }
//    加载
    private void initFragment() {
        mFriendsFragment = getSupportFragmentManager();
        mianfragment = new Mainfragment();
        myselffragment = new Myselffragment();
        productListFragment = new ProductListFragment();
    }
//    改变显示的网页
    public void changeFragment(Fragment to) {
        if (mContentFragment != null && mContentFragment != to) {
            if (!to.isAdded()) {
                mFriendsFragment.beginTransaction().hide(mContentFragment)
                        .add(R.id.fragment_content, to).commitAllowingStateLoss();
            } else {
                mFriendsFragment.beginTransaction().hide(mContentFragment)
                        .show(to).commitAllowingStateLoss();
            }
            mContentFragment = to;
        }
    }
}